package com.guyaning.deyson_core.core.net;

import android.content.Context;

import com.guyaning.deyson_core.core.net.callback.IError;
import com.guyaning.deyson_core.core.net.callback.IFailure;
import com.guyaning.deyson_core.core.net.callback.IRequest;
import com.guyaning.deyson_core.core.net.callback.ISuccess;
import com.guyaning.deyson_core.core.net.callback.RequestCallBacks;
import com.guyaning.deyson_core.core.net.download.DownLoadHandler;
import com.guyaning.deyson_core.core.ui.loader.LatteLoader;
import com.guyaning.deyson_core.core.ui.loader.LoaderStyle;

import java.io.File;
import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * Created by Administrator on 2018/4/10.
 */

public class RestClient {

    private final String URL;

    private static final WeakHashMap<String, Object> PARAMS = RestCreator.getParams();

    private final String DOWNLOAD_DIR;

    private final String EXTENSION;

    private final String NAME;

    private final IRequest REQUEST;

    private final ISuccess SUCCESS;

    private final IError ERROR;

    private final IFailure FAILURE;

    private final RequestBody BODY;

    private final LoaderStyle LOADERSTYLE;

    private final Context CONTEXT;

    private final File FILE;

    public RestClient(String url,
                      Map<String, Object> params,
                      String downloadDir,
                      String extension,
                      String name,
                      IRequest request,
                      ISuccess success,
                      IError error,
                      RequestBody body,
                      IFailure failure,
                      File file,
                      LoaderStyle loaderStyle,
                      Context context
                      ) {
        this.URL = url;
        PARAMS.putAll(params);
        this.DOWNLOAD_DIR = downloadDir;
        this.EXTENSION = extension;
        this.NAME = name;
        this.REQUEST = request;
        this.SUCCESS = success;
        this.ERROR = error;
        this.BODY = body;
        this.FAILURE = failure;
        this.FILE = file;
        this.LOADERSTYLE  = loaderStyle;
        this.CONTEXT = context;
    }

    public static RestClientBuilder builder() {
        return new RestClientBuilder();
    }

    private void request(HttpMethod method) {
        final ResetService service = RestCreator.getResetService();
        Call<String> call = null;

        if (REQUEST != null) {
            REQUEST.onRequestStart();
        }

        if(LOADERSTYLE != null){
            LatteLoader.showLoading(CONTEXT,LOADERSTYLE);
        }

        switch (method) {
            case GET:
                call = service.get(URL, PARAMS);
                break;
            case POST:
                call = service.post(URL, PARAMS);
                break;

            case POST_RAW:
                call = service.postRaw(URL,BODY);
                break;

            case PUT:
                call = service.put(URL, PARAMS);
                break;

            case PUT_RAW:
                call = service.putRaw(URL,BODY);
                break;
            case UPLOAD:
                final RequestBody requestBody =
                        RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()),FILE);

                final MultipartBody.Part body = MultipartBody.Part.createFormData("file",FILE.getName(),requestBody);

                call = RestCreator.getResetService().upload(URL,body);

                break;

            case DELETE:
                call = service.delete(URL, PARAMS);
                break;

            default:
                break;
        }

        if (call != null) {
            call.enqueue(getRequestCallBack());
        }
    }

    private Callback<String> getRequestCallBack() {
        return new RequestCallBacks(
                REQUEST,
                SUCCESS,
                ERROR,
                FAILURE,
                LOADERSTYLE
        );
    }

    public final void get() {
        request(HttpMethod.GET);
    }

    public final void post() {
        if(BODY == null){
            request(HttpMethod.POST);
        }else {
            if(!PARAMS.isEmpty()){
                throw new RuntimeException("params must be null");
            }
            request(HttpMethod.POST_RAW);
        }
    }

    public final void put() {
        if(BODY == null){
            request(HttpMethod.PUT);
        }else {
            if(!PARAMS.isEmpty()){
                throw new RuntimeException("params must be null");
            }
            request(HttpMethod.PUT_RAW);
        }
    }

    public final void delete() {
        request(HttpMethod.DELETE);
    }

    public final void download() {
        new DownLoadHandler(URL,DOWNLOAD_DIR, EXTENSION, NAME,REQUEST,
                SUCCESS,ERROR,FAILURE)
                .handlerDownLoad();
    }

}
